//
//  UIBarButtonItem.swift
//  weibo
//
//  Created by Limonwei on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

//抽象tabbar控件Item按钮的属性 

import UIKit

//item字体颜色
let ItemTitleColor:UIColor = UIColor(white: 80/255, alpha: 1)

//字体大小
let ItemTitleFontSize:CGFloat = 14

extension UIBarButtonItem{
    
    //便利构造函数 初始化对象  可以返回nil
    convenience init(imgName: String? = nil, title: String? = nil
        , target: AnyObject?, action: Selector) {
        self.init()
            
            //定义一个按钮
            let button = UIButton()
            
            //添加点击事件 (此处应该添加target,初始化的时候是target)
            button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
            
            //设置item的图片
            if let img = imgName {
                
                button.setImage(UIImage(named: img), forState: .Normal)
                
                button.setImage(UIImage(named: "\(img)_highlighted"), forState: .Selected)
            }
            
            //设置item按钮的title
            if let t = title {
                
                button.setTitle(t, forState: .Normal)
                
                
                button.setTitleColor(ItemTitleColor, forState: .Normal)
                
                button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
                
                button.titleLabel?.font = UIFont.systemFontOfSize(ItemTitleFontSize)
                
            }
            
            //自动调整大小
            button.sizeToFit()
            
            //把nav的customView属性赋值给button
            customView = button
            
            
    }
    
}



















